package com.ecarx.library_base.base_view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

import com.ecarx.library_base.base_util.DebounceClickListener;

/**
 * @author : kjn
 * @date : 19-5-7 上午10:18
 * Description : 触摸时显示50%的透明度
 */
public class ThemeTextView extends AppCompatTextView {

    private boolean isActionDown;


    public ThemeTextView(Context context) {
        super(context);
    }

    public ThemeTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ThemeTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        isActionDown = event.getAction() == MotionEvent.ACTION_DOWN;
        return super.onTouchEvent(event);
    }

    @Override
    protected void dispatchSetPressed(boolean pressed) {
        if (isClickable() && pressed && isActionDown) {
            setAlpha(0.5f);
        } else {
            setAlpha(1f);
        }
        isActionDown = false;
        super.dispatchSetPressed(pressed);
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        setAlpha(enabled ? 1f : 0.5f);
    }

    @Override
    public void setOnClickListener(@Nullable OnClickListener l) {
        DebounceClickListener l1 = new DebounceClickListener(l);
        super.setOnClickListener(l1);
    }
}
